package com.bookmanager.www.controller;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: 风离
 * @Date: 2021/09/19/10:13
 * @Description:
 */

import com.bookmanager.www.common.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Controller
public class TestController {

    @RequestMapping("/hello")
    public String hello(HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        return "hello";
    }

    @RequestMapping("/login")
    public ResponseEntity<Map<String, String>> login(String username, String password, HttpServletResponse response) {
        log.info("username:{},password:{}",username,password);
        Map<String, String> map = new HashMap<>();
        if (!"tom".equals(username) || !"123".equals(password)) {
            map.put("msg", "用户名密码错误");
            return ResponseEntity.ok(map);
        }
        JwtUtils jwtUtil = new JwtUtils();
        Map<String, Object> chaim = new HashMap<>();
        chaim.put("username", username);

        String jwtToken = jwtUtil.encode(username, 500 * 60 * 1000);
        map.put("msg", "登录成功");
        map.put("token", jwtToken);
        // 登录之后 你的浏览器header 会有这两个 Authorization ||| Access-Control-Expose-Headers

        response.setHeader("Authorization", jwtToken);
        response.setHeader("Access-Control-Expose-Headers", "Authorization");
        return ResponseEntity.ok(map);
    }
    @RequestMapping("/testdemo")
    public ResponseEntity<String> testdemo() {
        return ResponseEntity.ok("风离测试");
    }



        @Autowired
        private RabbitTemplate rabbitTemplate;

        @Value("${mayikt.order.exchange}")
        private String orderExchange; //订单交换机

        @Value("${mayikt.order.routingKey}")
        private String orderRoutingKey; //订单路由key

        @GetMapping("/addOrder")
        @ResponseBody
        public String addOrder(){
            log.warn("进入添加订单方法中了" +
                    "");
            String orderId="110812521016363";
            //rabbit投递消息
            rabbitTemplate.convertAndSend(orderExchange,orderRoutingKey,orderId,messagePostProcessor());
            return "success";
        }

        //处理待发送消息
        private MessagePostProcessor messagePostProcessor(){
            return  new MessagePostProcessor() {
                @Override
                public Message postProcessMessage(Message message) throws AmqpException {
                    //设置有效期30分钟
                    //message.getMessageProperties().setExpiration("1800000");
                    message.getMessageProperties().setExpiration("20000");
                    return message;
                }
            };
        }


}
